package yinxiang;

import com.evernote.edam.type.Note;
import com.evernote.edam.type.Notebook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xwpf.usermodel.*;

public class Test {

    private static Map<String,String> dayNoteBooks = new LinkedHashMap(){{
        put("2020日记","9ee0c7f3-797b-4af5-8be5-fe0eae8f8f77");
        put("2021日记","773d6473-1af8-448e-908d-03d7eb4dbdb1");
        put("2022日记","87dcacf6-6b06-4e03-b323-b53f18c22220");
        put("2023日记","5be9449a-16b2-4923-8d34-cb6f7f96301a");
    }};

    public static void main(String[] args) throws Exception {
        Api api = new Api();
//        noteBooks(api);
//        note(api);
        toWord(api);
    }

    private static void toWord(Api api) throws Exception {
        int i = 0;
        XWPFDocument document = new XWPFDocument();

        for(Map.Entry<String,String> entry : dayNoteBooks.entrySet()){

            // 设置页面分隔符
            if (i > 0) {
                XWPFParagraph pageBreak = document.createParagraph();
                pageBreak.setPageBreak(true);
            }
            // 设置段落格式
            XWPFParagraph yearParagraph = document.createParagraph();
            yearParagraph.setStyle("Normal");
            yearParagraph.setSpacingBetween(4, LineSpacingRule.AUTO);
            yearParagraph.setAlignment(ParagraphAlignment.LEFT);

            // 添加段落内容
            XWPFRun yRun = yearParagraph.createRun();
            // 设置字体
            yRun.setFontFamily("汉仪青云简");
            // 设置字体大小
            yRun.setFontSize(20);
            yRun.setText("嘿，还记得这年吗？\r" +
                    "欢迎来到不达意的" + entry.getKey().replaceAll("日记","") + "！");

            List<Note> notes = api.notes(entry.getValue());

            i++;
            for(Note note : notes){
                // 设置页面分隔符
                XWPFParagraph pageBreak = document.createParagraph();
                pageBreak.setPageBreak(true);

                // 创建标题
                XWPFParagraph titleParagraph = document.createParagraph();
                titleParagraph.setAlignment(ParagraphAlignment.CENTER);
                titleParagraph.setSpacingBetween(3);
                XWPFRun titleRun = titleParagraph.createRun();
                titleRun.setText(Util.reduceSpaces(note.getTitle()));
                titleRun.setBold(true);
                titleRun.setFontSize(16);
                titleRun.setFontFamily("汉仪青云简");

                // 设置段落格式
                XWPFParagraph paragraph = document.createParagraph();
                paragraph.setStyle("Normal");
                paragraph.setSpacingBetween(1.5, LineSpacingRule.AUTO);
                paragraph.setVerticalAlignment(TextAlignment.BASELINE);
                // 添加段落内容
                XWPFRun run = paragraph.createRun();
                // 设置字体
                run.setFontFamily("楷体");
                // 设置字体大小
                run.setFontSize(12);
                Note fullNote = api.note(note.getGuid(), true);
                run.setText(Util.removeHtmlTags(fullNote.getContent()));

            }
        }

        try (FileOutputStream out = new FileOutputStream("output.docx")) {
            document.write(out);
            System.out.println("Word生成成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void noteBooks(Api api) throws Exception {
        List<Notebook> notebooks = api.notebooks();
        for (Notebook notebook : notebooks){
            System.out.println(notebook);
        }
    }

    private static void note(Api api) throws Exception {
        //获取所有日记
        for(Map.Entry<String,String> entry : dayNoteBooks.entrySet()){
            System.out.println("--------  " + entry.getKey() + "  --------");
            List<Note> notes = api.notes(entry.getValue());
            for(Note note : notes){
                System.out.println(Util.reduceSpaces(note.getTitle()));
                Note fullNote = api.note(note.getGuid(), true);
                System.out.println(fullNote.getContent());
            }
        }
    }
}
